package com.my.springbootexcel.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.springbootexcel.bean.ExcelBean;
import com.my.springbootexcel.mapper.ExcelMapper;
import com.my.springbootexcel.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: marshal
 * @date: 2021-08-03 4:19 PM
 */
@Service
@Slf4j
public class ExcelServiceImpl extends ServiceImpl<ExcelMapper, ExcelBean> implements ExcelService {

    @Override
    public void importExcel(MultipartFile multipartFile) {
        try (InputStream inputStream = multipartFile.getInputStream()) {
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            List<Map<String, Object>> list = reader.readAll();
            log.info("list => {}", list);
            List<ExcelBean> excelBeanList = list.stream().map(this::buildExcelBean).collect(Collectors.toList());
            log.info("data => {}", excelBeanList);
            saveBatch(excelBeanList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importExcelByBean(MultipartFile multipartFile) {
        try (InputStream inputStream = multipartFile.getInputStream()) {
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            List<ExcelBean> excelBeanList = reader.readAll(ExcelBean.class);
            saveBatch(excelBeanList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importExcelCustom(MultipartFile multipartFile) {
        try (InputStream inputStream = multipartFile.getInputStream()) {
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            // 第二行当做标题，从第三行开始读取
            List<Map<String, Object>> list = reader.read(1, 2, Integer.MAX_VALUE);
            log.info("importExcelCustom.list => {}", list);
            List<ExcelBean> excelBeanList = list.stream().map(this::buildExcelBean).collect(Collectors.toList());
            log.info("data => {}", excelBeanList);
            saveBatch(excelBeanList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  构建ExcelBean对象
     *
     * @param map excel 数据键值对
     * @return ExcelBean
     */
    private ExcelBean buildExcelBean(Map<String, Object> map) {
        return ExcelBean.builder()
                .name(objToString(map.get("姓名")))
                .age(Integer.parseInt(map.get("年龄").toString()))
                .mobile(objToString(map.get("电话")))
                .email(objToString(map.get("邮箱")))
                .address(objToString(map.get("地址")))
                .build();
    }

    private String objToString(Object object) {
        if (Objects.nonNull(object)) {
            return object.toString();
        }
        return null;
    }
}
